package com.gxj.study.shopdemo.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.daimajia.slider.library.Animations.DescriptionAnimation;
import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.BaseSliderView;
import com.daimajia.slider.library.SliderTypes.TextSliderView;
import com.daimajia.slider.library.Tricks.ViewPagerEx;
import com.google.gson.Gson;
import com.gxj.study.shopdemo.Contants;
import com.gxj.study.shopdemo.R;
import com.gxj.study.shopdemo.WareListActivity;
import com.gxj.study.shopdemo.adapter.decoration.CardViewDecortion;
import com.gxj.study.shopdemo.adapter.HomeCatgoryAdapter;
import com.gxj.study.shopdemo.bean.Banner;
import com.gxj.study.shopdemo.bean.Campaign;
import com.gxj.study.shopdemo.bean.HomeCampaign;
import com.gxj.study.shopdemo.http.BaseCallback;
import com.gxj.study.shopdemo.http.OkHttpHelper;
import com.gxj.study.shopdemo.http.SpotsCallBack;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.util.List;


/**
 * Created by 祥杰 on 2016/7/31.
 */

public class HomeFragment extends BaseFragment {

    @ViewInject(R.id.slider)
    private SliderLayout mSlideLayout;
    @ViewInject(R.id.recyclerView)
    private RecyclerView mRecyclerView;

    private HomeCatgoryAdapter mAdapter;
    private List<Banner> mBanner;
    private Gson mGson = new Gson();
    private OkHttpHelper httpHelper = OkHttpHelper.getmInstance();

    private static final String TAG = "HomeFragment";

    @Override
    public View createView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState) {
        return inflater.inflate(R.layout.fragment_home, container, false);
    }

    @Override
    public void init() {
        requestImages();
        initRecyclerView();
    }


    private void initRecyclerView() {

        httpHelper.get(Contants.API.CAMPAIGN_HOME, new BaseCallback<List<HomeCampaign>>() {

            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onFailure(Request request, Exception e) {

            }

            @Override
            public void onResponse(Response response) {

            }

            @Override
            public void onSuccess(Response response, List<HomeCampaign> homeCampaigns) {
                initData(homeCampaigns);
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });
    }

    private void initData(List<HomeCampaign> homeCampaigns) {
        mAdapter = new HomeCatgoryAdapter(homeCampaigns, getActivity());
        mAdapter.setOnCampaignClickListener(new HomeCatgoryAdapter.OnCampaignClickListener() {
            @Override
            public void onClick(View view, Campaign campaign) {
                Intent intent = new Intent(getActivity(), WareListActivity.class);
                intent.putExtra(Contants.COMPAINGAIN_ID, campaign.getId());
                startActivity(intent);
            }
        });
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.addItemDecoration(new CardViewDecortion());
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity()));
    }

    private void initSlider() {
        if (mBanner != null) {
            for (Banner banner : mBanner) {
                TextSliderView textSliderView = new TextSliderView(this.getActivity());
                textSliderView.image(banner.getImgUrl());
                textSliderView.description(banner.getDescription());
                textSliderView.setScaleType(BaseSliderView.ScaleType.Fit);
                mSlideLayout.addSlider(textSliderView);
            }
        }
        mSlideLayout.setPresetIndicator(SliderLayout.PresetIndicators.Center_Bottom);
        mSlideLayout.setCustomAnimation(new DescriptionAnimation());
        mSlideLayout.setPresetTransformer(SliderLayout.Transformer.RotateUp);
        mSlideLayout.setDuration(3000);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mSlideLayout.stopAutoCycle();
    }

    private void requestImages() {
        String url = "http://112.124.22.238:8081/course_api/banner/query?type=1";
        httpHelper.get(url, new SpotsCallBack<List<Banner>>(getActivity()) {
            @Override
            public void onBeforeRequest(Request request) {

            }

            @Override
            public void onSuccess(Response response, List<Banner> banners) {
                mBanner = banners;
//                Log.e("banner" , banners.toString());
                initSlider();
            }

            @Override
            public void onError(Response response, int code, Exception e) {

            }
        });

    }
}
